﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;

namespace Aspfm.Main
{
    public class ConfigTool
    {
        private static DataSourceList dslist = null;
        private static DateTime lastwrite = DateTime.MinValue;

        public static bool SaveConn(DataSourceList dataSourceList)
        {
            if (dataSourceList != null)
            {
                string filename = BaseAshx.CurrentPath(false) + "\\bin\\DbConn.xml";
                if (File.Exists(filename))
                {
                    File.Delete(filename);
                }
                if (dataSourceList.dataList != null)
                {
                    foreach (DataSource data in dataSourceList.dataList)
                    {
                        data.User = MySecurity.SEncryptString(data.User);
                        data.Pwd = MySecurity.SEncryptString(data.Pwd);
                        data.ConnStr = MySecurity.SEncryptString(data.ConnStr);
                    }
                }
                if (dataSourceList.DataParamsList != null)
                {
                    foreach (DataParams param in dataSourceList.DataParamsList)
                    {
                        if (param.paramslist != null && param.IsSecret)
                        {
                            foreach (DataParamsInfo paraminfo in param.paramslist)
                            {
                                paraminfo.KeyValue = MySecurity.SEncryptString(paraminfo.KeyValue);
                            }
                        }
                    }
                }
                Aspfm.Main.SerializeHelper.Save(dataSourceList, filename);
                if (dataSourceList.dataList != null)
                {
                    foreach (DataSource data in dataSourceList.dataList)
                    {
                        data.User = MySecurity.SDecryptString(data.User);
                        data.Pwd = MySecurity.SDecryptString(data.Pwd);
                        data.ConnStr = MySecurity.SDecryptString(data.ConnStr);
                    }
                }
                if (dataSourceList.DataParamsList != null)
                {
                    foreach (DataParams param in dataSourceList.DataParamsList)
                    {
                        if (param.paramslist != null && param.IsSecret)
                        {
                            foreach (DataParamsInfo paraminfo in param.paramslist)
                            {
                                paraminfo.KeyValue = MySecurity.SDecryptString(paraminfo.KeyValue);
                            }
                        }
                    }
                }
                return true;
            }
            return false;
        }

        public static DataSourceList ReadConn()
        {
            string filename = BaseAshx.CurrentPath(false) + "\\bin\\DbConn.xml";
            if (!File.Exists(filename))
            {
                string filepath = BaseAshx.CurrentPath(false);
                filename = Directory.GetParent(filepath.Trim('/').Trim('\\')).FullName + "\\bin\\DbConn.xml";
            }
            if (File.Exists(filename))
            {
                DateTime filelastwrite = File.GetLastWriteTime(filename);
                if (filelastwrite == lastwrite)
                {
                    return dslist;
                }
                else 
                {
                    dslist = Aspfm.Main.SerializeHelper.Load(typeof(DataSourceList), filename) as DataSourceList;
                    if (dslist.dataList != null)
                    {
                        foreach (DataSource data in dslist.dataList)
                        {
                            data.User = Aspfm.Main.MySecurity.SDecryptString(data.User);
                            data.Pwd = Aspfm.Main.MySecurity.SDecryptString(data.Pwd);
                            data.ConnStr = Aspfm.Main.MySecurity.SDecryptString(data.ConnStr);
                        }
                    }
                    if (dslist.DataParamsList != null)
                    {
                        foreach (DataParams param in dslist.DataParamsList)
                        {
                            if (param.paramslist != null && param.IsSecret)
                            {
                                foreach (DataParamsInfo paraminfo in param.paramslist)
                                {
                                    paraminfo.KeyValue = Aspfm.Main.MySecurity.SDecryptString(paraminfo.KeyValue);
                                }
                            }
                        }
                    }
                    lastwrite = filelastwrite;
                }
                return dslist;
            }
            return null;
        }

        public static String getIpAddress()
        {
            string ip = "";
            if (System.Web.HttpContext.Current != null && System.Web.HttpContext.Current.Request != null)
            {
                if (System.Web.HttpContext.Current.Request.ServerVariables["HTTP_VIA"] != null) // using proxy 
                {
                    ip = System.Web.HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString(); // Return real client IP. 
                }
                else// not using proxy or can't get the Client IP 
                {
                    ip = System.Web.HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"].ToString(); //While it can't get the Client IP, it will return proxy IP. 
                }
            }
            return ip;
        }
        public static string[] GetFiles(string path, string searchPattern, SearchOption searchOption)
        {
            return Directory.GetFiles(path, searchPattern, SearchOption.AllDirectories);
        }
    }
}
